package com.whxy02.tolearn.app.controller;


import com.whxy02.tolearn.app.beans.User;
import com.whxy02.tolearn.app.service.UserService;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.concurrent.TimeUnit;

@RestController
@RequestMapping("/index")
public class Index {

    @Resource
    UserService userService;
    @Resource
    RedisTemplate<String,Object> redisTemplate;

    /**
     * 加载首页是访问的路由
     * @param request
     * @return
     */
    @RequestMapping("/mounted")
    public String index(HttpServletRequest request, HttpServletResponse response){

        Cookie[] cookies=request.getCookies();
        if(cookies!=null){
            for(Cookie cookie:cookies){
                if(cookie.getName().equals("sessionID")) {
                    if(redisTemplate.opsForValue().get("user:"+cookie.getValue())!=null){
                        User user= (User) redisTemplate.opsForValue().get("user:"+cookie.getValue());
                        if(user!=null)
                        return user.getUsername();
                    }
                }


            }

        }
        return "";

    }
}
